{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "URL: http://bokeh.pydata.org/en/latest/docs/gallery/stocks.html\n",
    "\n",
    "Most examples work across multiple plotting backends, this example is also available for:\n",
    "\n",
    "* [Bokeh - stocks example](../bokeh/stocks_example.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import holoviews as hv\n",
    "hv.extension('matplotlib')\n",
    "%output fig='svg' dpi=120"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Defining the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from holoviews.operation.timeseries import rolling\n",
    "from bokeh.sampledata.stocks import AAPL, GOOG, IBM, MSFT\n",
    "\n",
    "def get_curve(data, label=''):\n",
    "    df = pd.DataFrame(data)\n",
    "    df['date'] = df.date.astype(np.datetime64)\n",
    "    return hv.Curve(df, label=label, kdims=[('date', 'Date')],\n",
    "                    vdims=[('adj_close', 'Price')])\n",
    "\n",
    "hv.Dimension.type_formatters[np.datetime64] = '%Y'\n",
    "\n",
    "aapl = get_curve(AAPL, label='AAPL')\n",
    "goog = get_curve(GOOG, label='GOOG')\n",
    "ibm  = get_curve(IBM, label='IBM')\n",
    "msft = get_curve(MSFT, label='MSFT')\n",
    "\n",
    "avg_curve = rolling(aapl, rolling_window=30).relabel('Average')\n",
    "avg_scatter = hv.Scatter(avg_curve, label='close')\n",
    "\n",
    "color_cycle = hv.Cycle(values=['#A6CEE3', '#B2DF8A','#33A02C', '#FB9A99'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_opts = dict(aspect=1, fig_size=200, legend_position='top_left')\n",
    "\n",
    "hv.Store.options(backend='matplotlib').Curve = hv.Options('style', color=color_cycle)\n",
    "\n",
    "scatter_style = dict(alpha=0.2, size=4, color='darkgrey')\n",
    "curve_style = dict(color='navy')\n",
    "\n",
    "(aapl * goog * ibm * msft).opts(plot=plot_opts) +\\\n",
    "(avg_scatter(style=scatter_style) * avg_curve(style=curve_style)).opts(plot=plot_opts)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
